//===================================================================================
// $$
//
// Coder: Code Milker v1.0
// 0 - EntityName
// 1 - lower
//===================================================================================


namespace App.Data.SqlServer.Repositories
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Linq;
    using App.Model;

    public class JobPositionRepository : BaseRepository, IJobPositionRepository
    {
        public JobPositionRepository(IUnitOfWork unitOfWork)
            : base(unitOfWork)
        {
        }
        public void Create(JobPosition jobPosition)
        {
            this.GetDbSet<JobPosition>().Add(jobPosition);
            this.UnitOfWork.SaveChanges();
        }

        public JobPosition GetJobPosition(Guid jobPositionId)
        {
            return this.GetDbSet<JobPosition>()
                .Where(c => c.Id == jobPositionId)
                .Single();
        }

        public void Update(JobPosition updatedJobPosition)
        {
            this.GetDbSet<JobPosition>().Attach(updatedJobPosition);
            this.SetEntityState(updatedJobPosition, updatedJobPosition.Id == Guid.Empty
                ? EntityState.Added
                : EntityState.Modified);
            this.UnitOfWork.SaveChanges();
        }

        public void Delete(Guid jobPositionId)
        {
            JobPosition JobPositionToDelete = this.GetJobPosition(jobPositionId);
            this.GetDbSet<JobPosition>().Remove(JobPositionToDelete);
            this.UnitOfWork.SaveChanges();
        }

        public IQueryable<JobPosition> Find()
        {
            return this.GetDbSet<JobPosition>();
        }

        public IQueryable<JobPosition> GetAll()
        {
            return this.GetDbSet<JobPosition>()
                .Include("ProvinceLov")
                .Include("CityLov");
        }
    }
}
